﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageUploader.domain;
using System.DirectoryServices;

namespace ImageUploader.GUI
{
    public partial class ExportUsersForm : Form
    {
        private DomainController dc;

        private string csvFileLocation;
        private int cnIndex=-1;

        public ExportUsersForm(DomainController dc)
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            this.dc = dc;
            InitializeComponent();
            this.Load += new EventHandler(ExportUsersForm_Load);
            txtCSVLocation.TextChanged += new EventHandler(txtCSVLocation_TextChanged);
        }



        private void txtCSVLocation_TextChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = txtCSVLocation.Text.Length > 0;
        }

        private void ExportUsersForm_Load(object sender, EventArgs e)
        {
            FillPropperties();
        }

        private void FillPropperties()
        {

            using (DirectoryEntry de = dc.LDAPConnection.CreateDirectoryEntryWithDomainName())
            {
                List<string> propList = new List<string>();
                List<User> users = new List<User>();
                DirectorySearcher search = new DirectorySearcher(de, "(objectClass=User)");
                SearchResultCollection res =search.FindAll();
                foreach (SearchResult sr in res)
                {
                    Console.WriteLine(sr.ToString());
                    foreach (string s in sr.Properties.PropertyNames)
                        if (!propList.Contains(s))
                        {
                            if (!s.Equals("cn"))
                                propList.Add(s);                                                          
                        }
                }
            //    foreach (string s in propList)                
            //        lstPropperties.datas.Add(new ListViewItem(s));                
                lstPropperties.DataSource = propList;
            }       
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
           try
           {
               string[] selectedItems=new string[lstPropperties.SelectedItems.Count+1];
               selectedItems[0] = "cn";
               for (int i = 0; i < lstPropperties.SelectedItems.Count; i++)
                   selectedItems[i+1]=(string)lstPropperties.SelectedItems[i]; // i+1 because of insertion of "CN" at beginning.

               this.lblExporting.Visible = true;
               this.Update();
               this.progressBar1.Visible = true;

               ExportUsers eu = new ExportUsers(csvFileLocation, dc.LDAPConnection, selectedItems);
               eu.ProgressChanged += new Delegates.ProgressHandler(eu_ProgressChanged);
               eu.ExportUsersToCsv();
               MessageBox.Show("Successfully exported all users!");
               this.Close();
           }
           catch (Exception ex)
           {
               lblExporting.Visible = false;
               progressBar1.Visible = false;
               MessageBox.Show(ex.Message);
           }
        }

        private void eu_ProgressChanged(object sender, ProgressEventArgs pea)
        {
            progressBar1.Value = pea.Percentage;
            Application.DoEvents();
        }

        private void btnSelectCSV_Click(object sender, EventArgs e)
        {
               SaveFileDialog sfd = new SaveFileDialog();
               sfd.FileName = "Users_Active_directory.csv";
               sfd.Filter = "csv files (*.csv)|*.csv";
               if (sfd.ShowDialog() == DialogResult.OK)
               {
                   csvFileLocation = sfd.FileName;
               }
               txtCSVLocation.Text = csvFileLocation;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            for(int i=0;i<lstPropperties.Items.Count;i++)
                lstPropperties.SetSelected(i,true);
        }

        private void buttonDeselectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lstPropperties.Items.Count; i++)
                lstPropperties.SetSelected(i, false);
        }
    }
}
